今天要介紹的用法是在Dialog裡面加入一個Recyclerview,馬上來看一下怎麼使用
首先先創一個dialog的layout,用來自定義dialog的樣式,這邊就簡單做一個
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:text="Dialog"
android:gravity="center"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7" />
</LinearLayout>
然後再創立一個layout,用來設定Recyclerview的樣式
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="分數:"
android:textSize="20sp" />
<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="TextView"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
/>
接著就回到程式碼部分,一樣要做一個adapter去綁定
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<HashMap<String,String>> dataList;
public MyAdapter(ArrayList<HashMap<String,String>> dataList) {
this.dataList = dataList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// 創建 ViewHolder 並連結項目佈局檔案
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
// 在這裡綁定數據到 ViewHolder 的 View
String itemView = String.valueOf(dataList.get(position));
holder.textView5.setText(dataList.get(position).get("score"));
}
@Override
public int getItemCount() {
return dataList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView textView5;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView5 = itemView.findViewById(R.id.textView5);
}
}
}
public class MainActivity extends AppCompatActivity {
private Dialog dialog;
private RecyclerView recyclerView;
private MyAdapter myAdapter;
private ArrayList<HashMap<String,String>> dataList;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog = new Dialog(this);
dialog.setContentView(R.layout.recyclerview_dialog); // 設置對話框的佈局
recyclerView = dialog.findViewById(R.id.recyclerview);
dataList = new ArrayList<>();
for(int i =0;i<=29;i++){
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put("score",String.valueOf(new Random().nextInt(80) + 20));
dataList.add(hashMap); // 添加到數據列表中
}
myAdapter = new MyAdapter(dataList);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 設置RecyclerView的佈局管理器
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.show(); // 點擊按鈕時顯示對話框
}
});
}
}
結果如下